package com.itheima.mobilesafe69.activity;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.utils.ConstantValue;
import com.itheima.mobilesafe69.utils.PrefUtils;
import com.itheima.mobilesafe69.utils.ToastUtil;
import com.itheima.mobilesafe69.view.SettingItemView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

public class Setup2Activity extends BaseSetupActivity {
	private SettingItemView siv_bound_sim;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup2);
		initUI();
	}
	
	private void initUI() {
		siv_bound_sim = (SettingItemView) findViewById(R.id.siv_bound_sim);
		
		//进入应用的时候,通过sp中是否保存了序列卡号,用作是否绑定的判断标示
		String simSerialNumber = PrefUtils.getString(this,ConstantValue.SIM_SERIAL_NUMBER, "");
		if(TextUtils.isEmpty(simSerialNumber)){
			siv_bound_sim.setCheck(false);
		}else{
			siv_bound_sim.setCheck(true);
		}
		siv_bound_sim.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//1,获取当前条的原有状态
				boolean check = siv_bound_sim.isCheck();
				//2,对第一步获取的状态取反
				siv_bound_sim.setCheck(!check);
				//3,存储绑定状态(绑定SIM卡,sim卡号存储在本地,sim卡把号码存储在本地,则认为已经绑定,如果没有存储,则认为没有绑定)
				if(!check){
					//获取sim卡的序列号,绑定
					String simNumber = getSimNumber();
					//存储到sp中
					PrefUtils.putString(Setup2Activity.this,ConstantValue.SIM_SERIAL_NUMBER, simNumber);
				}else{
					//不绑定,将保存寻列卡号的key节点将其移除掉 
					PrefUtils.removeKey(Setup2Activity.this,ConstantValue.SIM_SERIAL_NUMBER);
				}
			}
		});
	}

	/**
	 * 获取sim卡的序列号	权限
	 */
	protected String getSimNumber() {
		//1,获取电话管理者对象
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		//2,获取此电话关联的sim卡序列号
		return tm.getSimSerialNumber();
	}

	@Override
	public void showNextPage() {
		String sim_serial_number = PrefUtils.getString(this,ConstantValue.SIM_SERIAL_NUMBER, "");
		if(!TextUtils.isEmpty(sim_serial_number)){
			startActivity(new Intent(this, Setup3Activity.class));
			finish();
		}else{
			ToastUtil.show(this,"必须绑定sim卡");
		}
		
		//下一页移入一出动画
		overridePendingTransition(R.anim.next_in_translate, R.anim.next_out_translate);
	}

	@Override
	public void showPrePage() {
		startActivity(new Intent(this, Setup1Activity.class));
		finish();
		
		//上一页移入一出动画
		overridePendingTransition(R.anim.pre_in_translate, R.anim.pre_out_translate);
	}
}
